	
package file_manager;

import org.lobobrowser.html.UserAgentContext;
import org.lobobrowser.html.parser.HtmlParser;
import org.lobobrowser.html.test.SimpleUserAgentContext;
import org.w3c.dom.*;

import javax.xml.xpath.*;
import javax.xml.parsers.*;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.xml.sax.SAXException;


public class Article_Extractor {

	public void prova() throws ParserConfigurationException, SAXException,
			IOException, XPathExpressionException {
		
		Document document = null;
		String fw_path = "src/main/resources/dictionaries/filetemporaneo.txt";
		FileReader fr = new FileReader(
		"src/main/resources/dictionaries/confronto.txt");
		FileWriter fwTest = new FileWriter("src/main/resources/dictionaries/filefinale.txt");
		

		
		BufferedReader br = new BufferedReader(fr);
		
		String s;
		
		while ((s = br.readLine()) != null) {
			FileWriter fw = new FileWriter(fw_path);
			fw.write(s);
			
			
		
		 document = this.getHtmlParsedDocument(fw_path);
		
		XPath xpath = XPathFactory.newInstance().newXPath();
		
		NodeList nodes = (NodeList) xpath.evaluate(
				"//span[@id='divAdnetKeyword']", document,
				XPathConstants.NODESET);
		
		Element element = (Element) nodes.item(0);
		
		NodeList nodes2 = (NodeList) xpath.evaluate(
				"//div[@id='article_headline']", document,
				XPathConstants.NODESET);
		
		Element element2 = (Element) nodes2.item(0);
		
		
		
		if (element != null && element2 !=null) {
			System.out.println(element2.getTextContent() +" "+ element.getTextContent());
			fwTest.append(s.substring(0, 9)+ element2.getTextContent() +" "+ element.getTextContent() + "\n");
		}
		fw.close();
		;
		}
		fwTest.close();
		fr.close();
		
		
	}

	private Document getHtmlParsedDocument(String fwPath)
			throws ParserConfigurationException, IOException, SAXException {
		
		Logger.getLogger("org.lobobrowser").setLevel(Level.WARNING);
		UserAgentContext uacontext = new SimpleUserAgentContext();
		
		// In this case we will use a standard XML document
		// as opposed to Cobra's HTML DOM implementation.
		
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		Document document = null;
		DocumentBuilder builder = factory.newDocumentBuilder();
	
			File f = new File(fwPath);
			
			FileInputStream file = new FileInputStream(f);
			
			Reader reader = new InputStreamReader(file);
			
			document = builder.newDocument();
			// Here is where we use Cobra's HTML parser.
			HtmlParser parser = new HtmlParser(uacontext, document);
			parser.parse(reader);
		

		return document;
	}

	public static void main(String args[]) throws XPathExpressionException,
			ParserConfigurationException, SAXException, IOException {
		Article_Extractor p = new Article_Extractor();
		p.prova();
	}
}
